package com.ericsson.enap.modules.ebmLog.task.vgc;

import com.ericsson.common.domain.CollectBizTypeEnum;
import com.ericsson.common.domain.NetTypeEnum;
import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.common.util.RedisCache;
import com.ericsson.enap.modules.ebmLog.entity.vgc.VgcNetEntity;
import com.ericsson.enap.modules.ebmLog.service.vgc.VgcEbmDownloadService;
import com.ericsson.enap.modules.ebmLog.thread.VgcEbmDownloadProcessThread;
import com.ericsson.enap.modules.sys.entity.EpcDict;
import com.ericsson.enap.modules.sys.service.EpcDictService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * VGCEBM原始文件下载处理
 */
@Component
public class VgcEbmDownloadTask implements ManualTask {
    private Logger log = LoggerUtils.Logger(LogFileName.VgcEbmDownLoadJob);

    @Autowired
    private VgcEbmDownloadService vgcEbmDownloadService;
    @Autowired
    private EpcDictService epcDictService;

    @Value("${collectorIp}")
    private String collectorIp;
    @Autowired
    private RedisCache redisCache;

    @Override
    public void execute() {
       List<VgcNetEntity> vgcNetEntities= vgcEbmDownloadService.findVgcNetEntityList(collectorIp, Integer.parseInt(CollectBizTypeEnum.VGCEBMDownLoad.value()), Integer.parseInt(NetTypeEnum.AMF.value()));
        Map<String,String> netTypeNameMap= getNetMap();
        String storePath=downloadBasePath();
        log.info("=======EnipNetelement list:"+vgcNetEntities.size());
        CountDownLatch latch = new CountDownLatch(vgcNetEntities.size());
       for(VgcNetEntity vgcNetEntity: vgcNetEntities) {
           String netType=netTypeNameMap.get(vgcNetEntity.getType().toString());
           VgcEbmDownloadProcessThread vgcEbmDownloadProcessThread=new VgcEbmDownloadProcessThread(vgcNetEntity,storePath+ File.separator+netType+ File.separator+vgcNetEntity.getId(),netType,latch,log,redisCache);
           new Thread(vgcEbmDownloadProcessThread).start();
       }
        try {
            latch.await();
            log.info("===== All netelements download ebm files end! =====");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private Map<String, String> getNetMap() {
        Map<String,String> netMap=new HashMap<>();
        netMap.put("1","AMF");
        return netMap;
    }

    /**
     * 获取下载文件存储的基础目录
     * @return
     */
    private String downloadBasePath(){
        EpcDict epcDict=new EpcDict();
        epcDict.setKeyName("EbmLogDownloadPath");
        epcDict= epcDictService.selectEpcDict(epcDict);
        if(null!=epcDict&&!"".equals(epcDict.getKeyValue())) {
            return epcDict.getKeyValue();
        }else{
            return "/opt";
        }
    }
}
